package com.zunipe.trainticketsystem.database.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zunipe.trainticketsystem.database.TrainTicketDBHelper;
import com.zunipe.trainticketsystem.database.entity.Train;

import java.util.ArrayList;
import java.util.List;

public class TrainDao {

    private final TrainTicketDBHelper dbHelper;

    public TrainDao(Context context) {
        dbHelper = new TrainTicketDBHelper(context);
    }

    public List<Train> findAll() {
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        List<Train> list = new ArrayList<>();
        try (Cursor cursor = database.rawQuery("SELECT * FROM trains", new String[]{})) {
            while (cursor.moveToNext()) {
                Integer id = cursor.getInt(0);
                String trainNumber = cursor.getString(1);
                String placeOfStart = cursor.getString(2);
                String placeOfEnd = cursor.getString(3);
                String startTime = cursor.getString(4);
                String endTime = cursor.getString(5);
                String type = cursor.getString(6);
                Double price = cursor.getDouble(7);
                Integer count = cursor.getInt(8);
                Train train = new Train(id, trainNumber, placeOfStart, placeOfEnd, startTime, endTime, type, price, count);
                list.add(train);
            }
        }
        return list;
    }

    public Train findById(int selectId) {
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        try (Cursor cursor = database.rawQuery("SELECT * FROM trains WHERE id = ?", new String[]{String.valueOf(selectId)})) {
            if (cursor.moveToNext()) {
                Integer id = cursor.getInt(0);
                String trainNumber = cursor.getString(1);
                String placeOfStart = cursor.getString(2);
                String placeOfEnd = cursor.getString(3);
                String startTime = cursor.getString(4);
                String endTime = cursor.getString(5);
                String type = cursor.getString(6);
                Double price = cursor.getDouble(7);
                Integer count = cursor.getInt(8);
                return new Train(id, trainNumber, placeOfStart, placeOfEnd, startTime, endTime, type, price, count);
            }
        }
        return null;
    }

    public Train findByTrainNumberAndType(String selectTrainNumber, String selectType) {
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        try (Cursor cursor = database.rawQuery("SELECT * FROM trains WHERE train_number = ? AND type = ?", new String[]{selectTrainNumber, selectType})) {
            if (cursor.moveToNext()) {
                Integer id = cursor.getInt(0);
                String trainNumber = cursor.getString(1);
                String placeOfStart = cursor.getString(2);
                String placeOfEnd = cursor.getString(3);
                String startTime = cursor.getString(4);
                String endTime = cursor.getString(5);
                String type = cursor.getString(6);
                Double price = cursor.getDouble(7);
                Integer count = cursor.getInt(8);
                return new Train(id, trainNumber, placeOfStart, placeOfEnd, startTime, endTime, type, price, count);
            }
        }
        return null;
    }

    public List<Train> findByTrainNumber(String selectTrainNumber) {
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        List<Train> list = new ArrayList<>();
        try (Cursor cursor = database.rawQuery("SELECT * FROM trains WHERE train_number = ?", new String[]{selectTrainNumber})) {
            while (cursor.moveToNext()) {
                Integer id = cursor.getInt(0);
                String trainNumber = cursor.getString(1);
                String placeOfStart = cursor.getString(2);
                String placeOfEnd = cursor.getString(3);
                String startTime = cursor.getString(4);
                String endTime = cursor.getString(5);
                String type = cursor.getString(6);
                Double price = cursor.getDouble(7);
                Integer count = cursor.getInt(8);
                Train train = new Train(id, trainNumber, placeOfStart, placeOfEnd, startTime, endTime, type, price, count);
                list.add(train);
            }
        }
        return list;
    }

    public void insert(Train train) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        database.execSQL("INSERT INTO trains(train_number, place_of_start, place_of_end, start_time, end_time, type, price, count) VALUES(?, ?, ?, ?, ?, ?, ?, ?)",
                new Object[]{train.getTrainNumber(), train.getPlaceOfStart(), train.getPlaceOfEnd(),
                        train.getStartTime(), train.getEndTime(), train.getType(), train.getPrice(), train.getCount()});
    }

    public void update(Train train) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        if (train.getTrainNumber() != null)
            values.put("train_number", train.getTrainNumber());
        if (train.getPlaceOfStart() != null)
            values.put("place_of_start", train.getPlaceOfStart());
        if (train.getPlaceOfEnd() != null)
            values.put("place_of_end", train.getPlaceOfEnd());
        if (train.getStartTime() != null)
            values.put("start_time", train.getStartTime());
        if (train.getEndTime() != null)
            values.put("end_time", train.getEndTime());
        if (train.getPrice() != null)
            values.put("price", train.getPrice());
        if (train.getCount() != null)
            values.put("count", train.getCount());
        database.update("trains", values, "id = ?", new String[]{String.valueOf(train.getId())});
    }

    public void delete(Train train) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        database.execSQL("DELETE FROM trains WHERE id = ?", new Object[]{train.getId()});
    }

    public void deleteAll() {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        database.execSQL("DELETE FROM trains", new Object[]{});
    }
}
